package com.yellowpage.wf;  
  
import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.util.List; 
import java.util.Date;
import java.sql.Timestamp;
import com.opensymphony.xwork2.ActionSupport;   
import com.yellowpage.wf.data.Resource;
import com.yellowpage.wf.service.ResourceService;
  
public class UploadAction extends ActionSupport {   
    //上传文件存放路径   
    private final static String UPLOADDIR = "F:/Project";   
    //上传文件集合   
    private List<File> file;   
    //上传文件名集合   
    private List<String> fileFileName;   
    //上传文件内容类型集合   
    private List<String> fileContentType; 
    private String attachmentId;
    private String workflowId;
    private Resource resource;
    private ResourceService resourceService;
  
    public List<File> getFile() {   
        return file;   
    }   
  
    public void setFile(List<File> file) {   
        this.file = file;   
    }   
  
    public List<String> getFileFileName() {   
        return fileFileName;   
    }   
  
    public void setFileFileName(List<String> fileFileName) {   
        this.fileFileName = fileFileName;   
    }   
  
    public List<String> getFileContentType() {   
        return fileContentType;   
    }   
  
    public void setFileContentType(List<String> fileContentType) {   
        this.fileContentType = fileContentType;   
    }   
    
    public String getAttachmentId() {
		return attachmentId;
	}

	public void setAttachmentId(String attachmentId) {
		this.attachmentId = attachmentId;
	}

	public String getWorkflowId() {
		return workflowId;
	}

	public void setWorkflowId(String workflowId) {
		this.workflowId = workflowId;
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public String execute() throws Exception {   
        for (int i = 0; i < file.size(); i++) {   
            //循环上传每个文件   
            uploadFile(i);   
        }   
        return "success";   
    }   
  
    //执行上传功能   
    private void uploadFile(int i) throws FileNotFoundException, IOException {   
        try {   
            InputStream in = new FileInputStream(file.get(i)); 
            String dir = "F:/Project";  
            String originalFileName = this.getFileFileName().get(i).toString(); //原始文件名
            String type = originalFileName.substring(originalFileName.lastIndexOf(".")+1); //文件类型
            String d = new Timestamp(System.currentTimeMillis()).toString();
            String finalFileName = d.substring(0,10).replaceAll("-","")+d.substring(11,19).replaceAll(":","")+d.substring(20);
            File uploadFile = new File(dir,finalFileName+"."+type);   
            OutputStream out = new FileOutputStream(uploadFile);  
            byte[] buffer = new byte[1024 * 1024];   
            int length;   
            while ((length = in.read(buffer)) > 0) {   
                out.write(buffer, 0, length);   
            } 
            Resource resource = new Resource();
            resource.setCreateTime(new Date());
            resource.setName(finalFileName);
            resource.setType(type);
            resource.setDir(dir+"/"+finalFileName+"."+type);
            resourceService.saveResource(resource);
            System.out.println("resourceId="+resource.getResourceId());
            in.close();   
            out.close();   
        } catch (FileNotFoundException ex) {   
            ex.printStackTrace();   
        } catch (IOException ex) {   
            ex.printStackTrace();   
        }   
    }   
//    public static void main(String[] args){
//    	String d = "111_1231.txt";
//    	String finalFileName = d.substring(0,d.lastIndexOf("."));
//    	System.out.println("d"+d);
//    	System.out.println("F"+finalFileName);
//	}
  
}  
